package cn.itcast.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

/**
 * @author 虎哥
 */
@Slf4j
@Configuration
public class FilterConfig {

    @Bean
    @Order(1)
    public GlobalFilter globalFilter1(){
        return (exchange, chain) -> {
            // 前置阶段
            log.warn("过滤器1 pre阶段执行");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                // 后置阶段
                log.warn("过滤器1  post阶段执行");
            }));
        };
    }

    @Bean
    @Order(2)
    public GlobalFilter globalFilter2(){
        return  (exchange, chain) -> {
            // 前置阶段
            log.warn("过滤器2 pre阶段执行");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                // 后置阶段
                log.warn("过滤器2  post阶段执行");
            }));
        };
    }

    @Bean
    @Order(3)
    public GlobalFilter globalFilter3(){
        return  (exchange, chain) -> {
            // 前置阶段
            log.warn("过滤器3 pre阶段执行");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                // 后置阶段
                log.warn("过滤器3  post阶段执行");
            }));
        };
    }
}
